/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sociesc.indoorgps;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.sociesc.indoorgps.dao.Dao;
import com.sociesc.indoorgps.utils.DatabaseUtils;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import static spark.Spark.*;
/**
 *
 * @author guilherme
 */
public class Main {
    
    
    JsonArray wifiJson = new JsonArray();
    
    public static void main(String[] args){
        
        try {
            Logger.getLogger(Main.class.getName()).log(Level.INFO, "Criando banco de dados");
            DatabaseUtils.seedDatabase();
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        get("/retorno", (request, response) -> {
            JsonObject json = new JsonObject();
            
            try {
                Dao<User> userDao = new Dao(User.class);
                List<User> users = userDao.findAll();
                JsonArray usersJson = new JsonArray();
                for(User u : users){
                    JsonObject uJson = new JsonObject();
                    uJson.add("id", u.getId());
                    uJson.add("name", u.getName());
                    usersJson.add(uJson);
                }
                json.add("users", usersJson);
                        
            } catch (SQLException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                json.add("error", ex.getMessage());
                response.status(500);
            }
            
            return json.toString();
            //response.type("text/html");
            /*return "" + 
                "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" + 
                "<html>" +
                "<head>" +
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">" +
                "<title>DemoWeb</title>" +
                "</head>" +
                "<body>" +
                    "<center>" +
                        "<p>&nbsp;</p>" +
                        "<p>&nbsp;</p>" +
                        "<p>&nbsp;</p>" +
                        "<p>&nbsp;</p>" +
                        "<p>&nbsp;</p>" +
                        "<p class=\"big\">Objetos do chamado</p>" +
                    "</center>" +
                "</body>" +
                "</html>";*/
        });
        
        post("/", (request, response) -> {
            // .. Create something ..
            
            //JsonArray wifiJson = new JsonArray();
            
            /*HelloSpark.wifiJson = request.params(":wifi");
            
            JsonObject wJson = new JsonObject();
            wJson.add("ssid", u.getSsid());
            wJson.add("BSSID", u.getBssid());
            wJson.add("RSSI", u.getRssi());
            
            HelloSpark.wifiJson.add(wJson);*/
            
            response.status(201); // 201 Created
            response.type("text/html");
            return "" + 
                "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">" + 
                "<html>" +
                "<head>" +
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">" +
                "<title>DemoWeb</title>" +
                "</head>" +
                "<body>" +
                    "<center>" +
                        "<p class=\"big\">Retorno!</p>" +
                    "</center>" +
                "</body>" +
                "</html>";
        });
    }
    
}
